import { ClassSerializerInterceptor, UsePipes, ValidationPipe } from '@nestjs/common'
import { NestFactory, Reflector } from '@nestjs/core'
import { Validate } from './common/pipes/validate'
import { AppModule } from './app.module'
import { ValidateExceptionFilter } from './common/filter/vallidate.filter'
import { TransformInterceptor } from './common/TransformInterceptor/RequestTime.transform'
import { NestExpressApplication } from '@nestjs/platform-express'
import appConfig from './config/app.config'
import corsOptionsDelegate from './config/corsOptionsDelegate'

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule)
  // 异常管道与异常过滤器
  app.useGlobalPipes(new Validate({ whitelist: false, stopAtFirstError: true }))
  app.useGlobalFilters(new ValidateExceptionFilter())
  // 拦截器
  app.useGlobalInterceptors(new TransformInterceptor())
  // 静态资源访问
  app.useStaticAssets('uploads', { prefix: '/uploads' })
  // 字段序列化
  app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)))
  // 跨越相关配置
  app.enableCors(corsOptionsDelegate)

  await app.listen(Number(appConfig().app.port), () => {
    console.log(`成功，服务器启动在${appConfig().app.port}`)
  })
}
bootstrap()
